package com.yzc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.pojo.UserBean;
import com.yzc.mapper.UserBeanMapper;
import com.yzc.util.ResultData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class UserBeanServiceImpl extends ServiceImpl<UserBeanMapper, UserBean> {

    @Autowired
    private UserBeanMapper userBeanMapper;

    public Map login(String name, String pass) {
        Map map = new HashMap();
        //访问数据库
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", name);
        queryWrapper.eq("pass", pass);
        List<UserBean> list = userBeanMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            UserBean userBean = list.get(0);
            map.put("username", userBean.getName());
            map.put("userid", userBean.getId());
            map.put("result", "success");
        } else {
            map.put("result", "error");
        }
        return map;
    }

    public List<UserBean> loadaAll() {
        //操作
        return userBeanMapper.loadaAll();
    }

    public Map updateInfo(UserBean userBean) {
        Map map = new HashMap();

        try {
            userBeanMapper.updateInfo(userBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }

    public Map saveInfos(UserBean userBean) {
        Map map = new HashMap();
        try {
            // 检查用户名和密码是否为空
            if (userBean.getName() == null || userBean.getName().isEmpty() ||
                    userBean.getPass() == null || userBean.getPass().isEmpty()) {
                throw new IllegalArgumentException("用户名和密码不能为空");
            }
            // 首先插入用户
            userBeanMapper.saveUser(userBean);
            userBeanMapper.saveUserKz(userBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            map.put("msg", e.getMessage());
            throw new RuntimeException(e);
        }
        return map;
    }

    public List selectInfos(String name) {
        return userBeanMapper.selectInfosByWhere(name);
    }

    public UserBean getInfoById(int id) {
        return userBeanMapper.selectById(id);
    }

    public Map deleteInfo(UserBean userBean) {

        Map map = new HashMap();

        try {
            userBeanMapper.deleteByState(userBean);
            userBeanMapper.deleteRolesByState(userBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }


    public Map updatePass(UserBean userBean) {
        Map map = new HashMap();

        try {
            userBeanMapper.updatePass(userBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }

    public Map updateXinxi(UserBean userBean) {
        Map map = new HashMap();

        try {
            userBeanMapper.updateXinxi(userBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }


}




